package com;

import org.junit.Test;
import pojo.tb01;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

public class JDBCDemo05_ResultSet {
    @Test
    public void test01() throws Exception {
        //1.注册驱动
        Class.forName("com.mysql.jdbc.Driver");
        //2.获取连接
        String url = "jdbc:mysql://127.0.0.1:3306/db01";
        String user = "root";
        String password = "137867";
        Connection connection = DriverManager.getConnection(url, user, password);
        //3编写sql
        String sql = "select * from tb01";
        //4.执行sql对象
        Statement statement = connection.createStatement();
        //5.执行sql
        ResultSet resultSet = statement.executeQuery(sql);
        //6处理结果
        while (resultSet.next()) {
            int id = resultSet.getInt("id");
            String name = resultSet.getString("name");
            int number = resultSet.getInt("number");
            System.out.println(id + name + number);
        }
        //7关闭资源
        statement.close();
        connection.close();

    }


    @Test
    public void test02() throws Exception {
        //1.注册驱动
        Class.forName("com.mysql.jdbc.Driver");
        //2.获取连接
        String url = "jdbc:mysql://127.0.0.1:3306/db01";
        String user = "root";
        String password = "137867";
        Connection connection = DriverManager.getConnection(url, user, password);
        //3编写sql
        String sql = "select * from tb01";
        //4.执行sql对象
        Statement statement = connection.createStatement();
        //5.执行sql
        ResultSet resultSet = statement.executeQuery(sql);
        //6.处理结果
        List<tb01> tb01s = new ArrayList<>();
        tb01 tb = null;
        while (resultSet.next()) {
            tb=new tb01();
            int id = resultSet.getInt("id");
            String name = resultSet.getString("name");
            int number = resultSet.getInt("number");
            tb.setId(id);
            tb.setName(name);
            tb.setNumber(number);
            tb01s.add(tb);


        }
        System.out.println(tb01s);
        resultSet.close();
        statement.close();
        connection.close();
    }


}
